package week

import (
	"github.com/gin-gonic/gin"
	"menu-generation-go/database"
	"menu-generation-go/handlers/common"
	"menu-generation-go/models"
)

type RecipeHandler struct {
	DB *database.Database
}

func NewRecipeHandler(db *database.Database) *RecipeHandler {
	return &RecipeHandler{DB: db}
}

// ListRecipes 获取菜品列表
// @Summary 获取菜品列表
// @Description 获取菜品列表，支持类型过滤和搜索
// @Tags 菜品
// @Accept json
// @Produce json
// @Param type query int false "菜品类型 (1:大荤菜, 2:小荤菜, 3:素菜, 4:汤类)"
// @Param q query string false "搜索关键词"
// @Success 200 {object} common.Response{data=[]models.MenuRecipe}
// @Router /api/recipes [get]
func (h *RecipeHandler) ListRecipes(c *gin.Context) {
	recipeType := c.Query("type")
	search := c.Query("q")

	query := h.DB.DB.Model(&models.MenuRecipe{})

	if recipeType != "" {
		query = query.Where("type = ?", recipeType)
	}

	if search != "" {
		query = query.Where("name LIKE ?", "%"+search+"%")
	}

	var recipes []models.MenuRecipe
	result := query.Find(&recipes)
	if result.Error != nil {
		common.SendErrJSON("查询失败", c)
		return
	}

	common.Ok(c, recipes)
}
